package mapper

import "fmt"

// 等于
// 例如 clo = val
func (m MapperQuery) Eq(clo string, val any) *MapperQuery {

	return m.ConEq(true, clo, val)
}

// 条件等于，如果condition为false，则不添加条件
// 例如 clo = val
// val为地址类型
func (m MapperQuery) ConEq(condition bool, column string, val any) *MapperQuery {
	if !condition {
		return &m
	}
	var valStr []any

	valStr = append(valStr, val)
	//}
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "=", whereValue: valStr})

	return &m
}

// 不等于
// 例如 clo != val
func (m MapperQuery) Ne(column string, val any) *MapperQuery {

	return m.ConNe(true, column, val)
}

// 条件不等于，如果condition为false，则不添加条件
// 例如 clo!= val
func (m MapperQuery) ConNe(condition bool, column string, val any) *MapperQuery {
	if !condition {
		return &m
	}
	var valStr []any
	valStr = append(valStr, val)
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "<>", whereValue: valStr})
	return &m
}

func (m MapperQuery) Gt(column string, val any) *MapperQuery {

	return m.ConGt(true, column, val)
}
func (m MapperQuery) ConGt(condition bool, column string, val any) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, val)
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: ">", whereValue: valStr})
	return &m
}

func (m MapperQuery) Ge(column string, val any) *MapperQuery {

	return m.ConGe(true, column, val)
}

func (m MapperQuery) ConGe(condition bool, column string, val any) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, val)
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: ">=", whereValue: valStr})
	return &m
}

func (m MapperQuery) Lt(column string, val any) *MapperQuery {

	return m.ConLt(true, column, val)
}
func (m MapperQuery) ConLt(condition bool, column string, val any) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, val)
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "<", whereValue: valStr})
	return &m
}

func (m MapperQuery) Le(column string, val any) *MapperQuery {

	return m.ConLe(true, column, val)
}
func (m MapperQuery) ConLe(condition bool, column string, val any) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, val)
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "<=", whereValue: valStr})
	return &m
}

// 条件In
// 例如 clo IN (val1,val2,val3)
func (m MapperQuery) In(column string, val ...any) *MapperQuery {

	return m.ConIn(true, column, val...)
}

// 条件In，如果condition为false，则不添加条件
// 例如 clo IN (val1,val2,val3)
func (m MapperQuery) ConIn(condition bool, column string, val ...any) *MapperQuery {
	if !condition {
		return &m
	}

	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "IN", whereValue: val})
	return &m
}

// 条件NotIn
// 例如 clo NOT IN (val1,val2,val3)
func (m MapperQuery) NotIn(column string, val ...any) *MapperQuery {
	return m.ConNotIn(true, column, val...)
}

// 条件NotIn，如果condition为false，则不添加条件
// 例如 clo NOT IN (val1,val2,val3)
func (m MapperQuery) ConNotIn(condition bool, column string, val ...any) *MapperQuery {
	if !condition {
		return &m
	}

	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "NOT IN", whereValue: val})
	return &m
}

func (m MapperQuery) Between(column string, valStart any, valEnd any) *MapperQuery {

	return m.ConBetween(true, column, valStart, valEnd)
}

func (m MapperQuery) ConBetween(condition bool, column string, valStart any, valEnd any) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, valStart)
	valStr = append(valStr, valEnd)
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "BETWEEN", whereValue: valStr})
	return &m
}

func (m MapperQuery) NotBetween(column string, valStart any, valEnd any) *MapperQuery {

	return m.ConNotBetween(true, column, valStart, valEnd)
}
func (m MapperQuery) ConNotBetween(condition bool, column string, valStart any, valEnd any) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, valStart)
	valStr = append(valStr, valEnd)
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "NOT BETWEEN", whereValue: valStr})
	return &m
}

func (m MapperQuery) Or() *MapperQuery {

	return m.ConOr(true)
}
func (m MapperQuery) ConOr(condition bool) *MapperQuery {
	if !condition {
		return &m
	}

	m.whereMap = append(m.whereMap, WhereSql{columnName: "", whereType: "OR", whereValue: nil})
	return &m
}

func (m MapperQuery) OrGroup(qu MapperQuery) *MapperQuery {

	return m.ConOrGroup(true, qu)
}
func (m MapperQuery) ConOrGroup(condition bool, qu MapperQuery) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, qu)
	m.whereMap = append(m.whereMap, WhereSql{columnName: "", whereType: "OR", whereValue: valStr})
	return &m
}

func (m MapperQuery) AndGroup(qu MapperQuery) *MapperQuery {

	return m.ConAndGroup(true, qu)
}
func (m MapperQuery) ConAndGroup(condition bool, qu MapperQuery) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, qu)
	m.whereMap = append(m.whereMap, WhereSql{columnName: "", whereType: "AND", whereValue: valStr})
	return &m
}

func (m MapperQuery) Like(column string, val *string) *MapperQuery {
	return m.ConLike(true, column, val)

}
func (m MapperQuery) ConLike(condition bool, column string, val *string) *MapperQuery {
	if !condition {
		return &m
	}
	if val == nil {
		return &m
	}
	var valStr []any
	valStr = append(valStr, fmt.Sprintf("%%%s%%", *val))
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "LIKE", whereValue: valStr})
	return &m

}

func (m MapperQuery) NotLike(column string, val *string) *MapperQuery {
	return m.ConNotLike(true, column, val)
}

func (m MapperQuery) ConNotLike(condition bool, column string, val *string) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, fmt.Sprintf("%%%s%%", *val))
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "NOT LIKE", whereValue: valStr})
	return &m
}
func (m MapperQuery) IsNull(column string) *MapperQuery {
	return m.ConIsNull(true, column)
}
func (m MapperQuery) ConIsNull(condition bool, column string) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, "")
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "IS NULL", whereValue: valStr})
	return &m
}

func (m MapperQuery) IsNotNull(column string) *MapperQuery {
	return m.ConIsNotNull(true, column)
}
func (m MapperQuery) ConIsNotNull(condition bool, column string) *MapperQuery {
	if !condition {
		return &m
	}

	var valStr []any
	valStr = append(valStr, "")
	m.whereMap = append(m.whereMap, WhereSql{columnName: column, whereType: "IS NOT NULL", whereValue: valStr})
	return &m
}

func (m MapperQuery) WhereStr(str string) *MapperQuery {

	var valStr []any
	valStr = append(valStr, str)
	m.whereMap = append(m.whereMap, WhereSql{columnName: "", whereType: "", whereValue: valStr})
	return &m
}
